﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Fashion.Models;
using System.ComponentModel.DataAnnotations;

namespace Fashion.ViewModels
{
    public class OrderDetailModel
    {
        public OrderDetail orderDetails { get; set; }
        public List<Order> orderList = new List<Order>();
        public string productName;
        public string size;
        public string color;
        public string coupon;
        public string control;

        [Required(ErrorMessage = "Total Price is invalid")]
        public double TotalPrice;

        private FashionEntities1 fashionDB = new FashionEntities1();

        public OrderDetailModel()
        {
        }

        public OrderDetailModel(int orderID)
        {
            var orderModel = fashionDB.OrderDetails.Single(o => o.OrderID == orderID);
            this.orderDetails = (OrderDetail)orderModel;

            var product = fashionDB.Products.Single(p => p.ProductID == orderDetails.ProductID);
            if (product.Sex == "Female")
                this.control = "Women";
            else
                this.control = "Men";
            this.productName = product.ProductName;

            var color = fashionDB.Colors.Single(c => c.ColorID == orderDetails.ColorID);
            this.color = color.Name;

            var size = fashionDB.Sizes.Single(s => s.SizeID == orderDetails.SizeID);
            this.size = size.Name;

            if (orderDetails.CouponID != null)
            {
                var coupon = fashionDB.Coupons.Single(c => c.CouponID == orderDetails.CouponID);
                this.coupon = coupon.Description;
            }
            else
                this.coupon = "N/A";

            this.TotalPrice = fashionDB.Orders.Single(o => o.OrderID == orderID).TotalPrice;
        }

        public void ViewTodayOrders()
        {
            var orders = from order in fashionDB.Orders
                         where order.OrderDate == DateTime.Today
                         orderby order.OrderDate descending
                         select order;
            this.orderList = orders.ToList();

            foreach (var order in orders)
            {
                var detail = fashionDB.OrderDetails.Single(o => o.OrderID == order.OrderID);

                order.quantity = detail.Quantity;

                var product = fashionDB.Products.Single(p => p.ProductID == detail.ProductID);
                if (product.Sex == "Female")
                    order.control = "Women";
                else
                    order.control = "Men";
                order.productName = product.ProductName;

                var color = fashionDB.Colors.Single(c => c.ColorID == detail.ColorID);
                order.color = color.Name;

                var size = fashionDB.Sizes.Single(s => s.SizeID == detail.SizeID);
                order.size = size.Name;

                if (detail.CouponID != null)
                {
                    var coupon = fashionDB.Coupons.Single(c => c.CouponID == detail.CouponID);
                    order.coupon = coupon.Description;
                }
                else
                    order.coupon = "N/A";
            }
        }

        public void ViewAllOrders()
        {
            var orders = from order in fashionDB.Orders
                         orderby order.OrderDate descending
                         select order;
            this.orderList = orders.ToList();

            foreach (var order in orders)
            {
                var detail = fashionDB.OrderDetails.Single(o => o.OrderID == order.OrderID);

                order.quantity = detail.Quantity;

                var product = fashionDB.Products.Single(p => p.ProductID == detail.ProductID);
                if (product.Sex == "Female")
                    order.control = "Women";
                else
                    order.control = "Men";
                order.productName = product.ProductName;

                var color = fashionDB.Colors.Single(c => c.ColorID == detail.ColorID);
                order.color = color.Name;

                var size = fashionDB.Sizes.Single(s => s.SizeID == detail.SizeID);
                order.size = size.Name;

                if (detail.CouponID != null)
                {
                    var coupon = fashionDB.Coupons.Single(c => c.CouponID == detail.CouponID);
                    order.coupon = coupon.Description;
                }
                else
                    order.coupon = "N/A";
            }
        }

        public void GetOrder(int orderID)
        {
            var orderModel = fashionDB.OrderDetails.Single(o => o.OrderID == orderID);
            this.orderDetails = (OrderDetail)orderModel;

            var product = fashionDB.Products.Single(p => p.ProductID == orderDetails.ProductID);
            if (product.Sex == "Female")
                this.control = "Women";
            else
                this.control = "Men";
            this.productName = product.ProductName;

            var color = fashionDB.Colors.Single(c => c.ColorID == orderDetails.ColorID);
            this.color = color.Name;

            var size = fashionDB.Sizes.Single(s => s.SizeID == orderDetails.SizeID);
            this.size = size.Name;

            if (orderDetails.CouponID != null)
            {
                var coupon = fashionDB.Coupons.Single(c => c.CouponID == orderDetails.CouponID);
                this.coupon = coupon.Description;
            }
            else
                this.coupon = "N/A";

            this.TotalPrice = fashionDB.Orders.Single(o => o.OrderID == orderID).TotalPrice;
        }
    }
}